package day0213;

import java.util.Random;

public class SyncDemo1 {
    //public static Object obj1111 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Shop shop = new Shop();

        //Runnable方式创建匿名内部类线程对象。
        Thread t1 = new Thread( ()->{
            try {
                System.out.println("线程t1--王克晶--开始执行");
                Thread.sleep(900);
                synchronized (shop) {
                    //Thread.sleep(900);
                    System.out.println("王克晶线程-唤醒线程t2-范传奇");
                    /*Scanner scanner = new Scanner(System.in);
                    String str = scanner.nextLine();*/
                    System.out.println("王克晶线程-唤醒线程t2-范传奇");
                    shop.notify(); // 需要持有锁后再通知
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"王克晶");
        t1.start();



        Thread t2 = new Thread("范传奇"){
            public void run() {
                try {
                    System.out.println("线程t2--范传奇--开始执行");
                    synchronized (shop) {
                        System.out.println("线程t2--范传奇--进入等待");
                        shop.wait();
                        System.out.println("线程t2--范传奇--等待结束--------over-over(线程t2运行完毕)");
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        t2.start();





        /*synchronized (shop) {
            Scanner scanner = new Scanner(System.in);
            String str = scanner.nextLine();
            System.out.println("唤醒t2");
            shop.notify(); // 需要持有锁后再通知
            Thread.sleep(100);
        }*/

    }
}



class Room{
    public String name;
    public String person = "范传奇";
    public Room(String name){
        this.name = name;
    }

    public String toString() {
        return "Room{" +  "name='" + name + '\'' +  '}';
    }
}








class Shop{
    public Room[] rooms = new Room[1];
    public Shop(){
        StringBuilder temp = new StringBuilder("试衣间");
        for (int i = 0; i < 1; i++) {
            rooms[i] = new Room(temp.append(i).toString());
            temp.deleteCharAt(temp.length()-1);
        }
    }



    public void buy() throws InterruptedException {
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+"-刚进门...");
            Thread.sleep(1000);
            System.out.println(t.getName()+"-正在挑衣服...");
            Thread.sleep(1000);

            Random random = new Random();
            int index = random.nextInt(rooms.length);

            synchronized (this.rooms[index]){
                System.out.println(t.getName()+"-正在"+this.rooms[index].name+"试衣服...");
                Thread.sleep(3000);
                System.out.println(t.getName()+"-"+this.rooms[index].name+"试衣服结束...");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
